package com.s59.mp;

import com.s59.Service.BookService;
import com.s59.Service.BootOrderService;
import com.s59.entity.Book;
import com.s59.entity.BootOrder;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class XfzMp {
    @Autowired
    private BootOrderService bootOrderService;
    @Autowired
    private BookService bookService;

    @RabbitListener(queues = "s59-queue1")
    private void receive1(BootOrder mg){
        System.out.println("收到订单消息，创建订单"+mg.getOrderId());
        Book book = bookService.findById(mg.getBookId());
        mg.setMoney(book.getPrice().multiply(new BigDecimal("0.1")));
        bootOrderService.createOrder(mg);
    }

    @RabbitListener(queues = "s59-queue2")
    private void receive2(BootOrder mg){
        System.out.println("收到订单消息，修改库存"+mg.getOrderId());
        System.out.println("书籍id"+mg.getBookId());
        bookService.modifyStock(mg.getBookId(),-1);
    }


}
